<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\inspect\controller;

use think\Db;
use think\Validate;
use cmf\controller\PublicController;

class IndexController extends PublicController
{
    /**
     * 访问限制
     * @var array
     */
    protected $auth = '*';

    /**
     * 安装单状态-old
     * @var array
     */
    protected $install_status = [
        1 => '待接收',
        2 => '进行中',
        3 => '已完成',
        4 => '已拒收',
    ];

    /**
     * 安装单状态-new
     * @var array
     */
    protected $install_status_new = [
        0 => '未进场',
        1 => '已进场',
        2 => '隐蔽施工中',
        3 => '隐蔽施工已结束',
        4 => '调试',
        5 => '运行',
        'X' => '作废',
    ];

    /**
     * 构造方法
     * @param Request $request Request对象
     * @access public
     */
    public function __construct()
    {
        // 引入公共控制器的初始化
        parent::__construct();
    }

    /**
     * 选择安装队数据
     * @param []
     * @return []
     * @date 2019/12/13 3:51 PM
     */
    public function installationTeam()
    {
        // 获取经销商编码
        $jxs_code = $this->user['jxs_code'];
        // 获取安装商信息
        $azsInfo = Db::name('rl_et_azs')->where(['sap_server_code' => $jxs_code])->field('et_azs_id,server_code,server_name')->select()->toArray();
        // 获取经销商下所有的安装商
        foreach ($azsInfo as $key => $val) {
            $server_code[$key] = $val['server_code'];
        }
        // 安装队查询条件
        $azdMap['server_code'] = array('in', $server_code);
        // 获取安装队信息
        $azdInfo = Db::name('rl_et_azd')->where($azdMap)->field('et_azd_id,engineer_code,engineer_name')->select()->toArray();
        // 判断是否为其他用户
        if ($this->userType == 2) {
            // 获取用户姓名
            $data['user_name'] = $this->user['user_name'];
            // 获取用户职位
            $data['gw_name'] = $this->userTypeList[$this->user['gw']];
            // 经销商名称
            $data['jxs_mc'] = Db::name('rl_et_customer')->where(['jxsbm' => $jxs_code])->value('jxs_mc');
        }
        // 获取安装队的安装单总数
        foreach ($azdInfo as $k => $v) {
            // 查询总安装单数
            $count = Db::name('rl_hise_service_project')->where(['azd' => $v['engineer_code']])->count();
            // 赋值总数
            $azdInfo[$k]['azd_count'] = $count;
        }
        // 安装队列表
        $data['list'] = $azdInfo;
        // 数据返回
        $this->success('操作成功！', $data);
    }

    /**
     * 安装队巡检安装单
     * @param []
     * @return []
     * @date 2019/12/17 10:02 AM
     */
    public function installationMount()
    {
        // 验证的规则
        $rule = [
            'engineer_code' => 'require',
            'gczt' => 'require',
        ];
        // 错误的消息
        $msg = [
            'engineer_code.require' => '工程师代码不存在',
            'gczt.require' => '安装单状态不存在',
        ];
        // 获取的数据
        $data = [
            'engineer_code' => $this->request->get('engineer_code'), // 获取工程师代码
            'gczt' => $this->request->get('gczt/d', '2'), // 获取安装单状态
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 判断安装单状态
        if($data['gczt'] == '2'){
            // 安装单状态
            $map['gczt'] = ['in','0,1,2,3,4'];
            $map['zzdpdzt'] = 2;
        } else {
            // 安装单状态 已完成
            $map['gczt'] = 5;
        }
        // 经销商编码
        $map['jxs'] = $this->user['jxs_code'];
        // 安装队编码
        $map['azd'] = $data['engineer_code'];
        // 设置字段
        $field = 'azd.engineer_name,hise.yh_name,hise.fgsfwfzr,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.jxsxsy,hise.hise_service_project_id';
        // 获取安装单列表信息
        $list = Db::table('cmf_rl_hise_service_project')
            ->alias('hise')
            ->join('cmf_rl_et_azd azd', 'hise.azd = azd.engineer_code')
            ->where($map)
            ->field($field)
            ->select()
            ->toArray();
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['yyaz_sj'] = $val['yyaz_sj'] ? date('Y-m-d', $val['yyaz_sj']) : '';
            $provice = Db::name('rl_hise_area')->where('sf_code', $val['provice'])->value('sf_name');
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $val['city'])->value('dq_name');
            $qx = Db::name('rl_hise_qx_table')->where('qx_code', $val['qx'])->value('qx_name');
            $list[$key]['address'] = $provice . $city . $qx . $val['address'];
            unset($list[$key]['provice']);
            unset($list[$key]['city']);
            unset($list[$key]['qx']);
        }
        // 获取安装单状态数量
        $map['gczt'] = array('in','0,1,2,3,4');
        // 进行中
        $data['count_2'] = $this->installationMountCount($map);
        $map['gczt'] = 5;
        // 已完成
        $data['count_3'] = $this->installationMountCount($map);
        // 安装队列表
        $data['list'] = $list;
        // 数据返回
        $this->success('操作成功！', $data);
    }

    /**
     * 安装队巡检安装单状态数量
     * @param []
     * @return []
     * @date 2019/12/17 12:40 PM
     */
    protected function installationMountCount($map)
    {
        // 获取安装单数量
        $count = Db::table('cmf_rl_hise_service_project')
            ->alias('hise')
            ->join('cmf_rl_et_azd azd', 'hise.azd = azd.engineer_code')
            ->where($map)
            ->count();
        return $count;
    }

    /**
     * 安装单巡检评分
     * @param []
     * @return []
     * @date 2019/12/17 5:19 PM
     */
    public function inspectionScore()
    {
        // 获取巡检单ID
        $inspectId = $this->request->get('inspectId');
        // 判断巡检单ID是否存在
        if (empty($inspectId)) {
            // 获取评分表数据
            $inspect_score_list = Db::name('rl_inspect_score')
                ->where('status', 1)
                ->field('id,number,name,content,fraction')
                ->order('id ASC')
                ->select()
                ->toArray();
            // 评分表数据
            $data['list'] = $inspect_score_list;
            // 返回信息
            $this->success('操作成功！', $data);
        } else {
            // 获取评分数据
            $inspect_score_list = Db::name('rl_inspect_score')
                ->alias('s')
                ->join('rl_inspect_score_scoring i', 'i.sid = s.id')
                ->where('tid',$inspectId)
                ->field('s.id,s.number,s.name,s.content,s.fraction,i.deduction')
                ->order('id ASC')
                ->select()
                ->toArray();
            // 巡检单评分
            $data['list'] = $inspect_score_list;
            // 返回信息
            $this->success('操作成功！', $data);
        }
    }

    /**
     * 安装单巡检添加+编辑-开始
     * @param []
     * @return []
     * @date 2019/12/19 3:30 PM
     */
    public function inspectionAddView()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id' => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id' => $this->request->get('azd_id'), // 获取安装单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 获取项目信息
        $list = Db::name('rl_hise_service_project')
            ->where('azd_id', $data['azd_id'])
            ->where('hise_service_project_id', $data['hise_service_project_id'])
            ->field('yh_name,sj_one,provice,city,qx,address,jxs,azs,azd,qyfgs')
            ->find();
        // 获取地址信息
        $provice = Db::name('rl_hise_area')->where('sf_code', $list['provice'])->value('sf_name');
        $city = Db::name('rl_hise_dq_table')->where('dq_code', $list['city'])->value('dq_name');
        $qx = Db::name('rl_hise_qx_table')->where('qx_code', $list['qx'])->value('qx_name');
        // 赋值地址信息
        $list['location'] = $provice . $city . $qx . $list['address'];
        // 获取经销商
        $list['jxs_name'] = Db::name('rl_et_customer')->where(['jxsbm' => $list['jxs']])->value('jxs_mc');
        // 获取安装商
        $list['azs_name'] = Db::name('rl_et_azs')->where(['server_code' => $list['azs']])->value('server_name');
        // 获取安装队
        $azdInfo = Db::name('rl_et_azd')->where(['engineer_code' => $list['azd']])->field('engineer_name,phone')->find();
        // 安装队姓名
        $list['azd_name'] = $azdInfo['engineer_name'];
        // 安装队手机号
        $list['azd_phone'] = $azdInfo['phone'];
        // 获取分公司
        $list['fgs_name'] = Db::name('rl_company')->where(['orgdept' => $list['qyfgs']])->value('orgname');
        // 提出无用的信息
        unset($list['provice']);
        unset($list['city']);
        unset($list['qx']);
        unset($list['address']);
        unset($list['jxs']);
        unset($list['azs']);
        unset($list['azd']);
        unset($list['qyfgs']);
        // 赋值信息
        $list['hise_service_project_id'] = $data['hise_service_project_id'];
        $list['azd_id'] = $data['azd_id'];
        // 获取巡检单ID
        $inspectId = input('get.inspectId');
        // 判断巡检ID是否存在
        if($inspectId){
            // 获取巡检单
            $xunj = Db::name('rl_inspect')
                ->where('hid',$data['hise_service_project_id'])
                ->where('azd_id',$data['azd_id'])
                ->where('uid',$this->userId)
                ->where('id',$inspectId)
                ->field('inspect_code,inspect_status')
                ->find();
            $inspect_code = $xunj['inspect_code'];
//            $inspect_code = Db::name('rl_inspect')
//                ->where('hid',$data['hise_service_project_id'])
//                ->where('azd_id',$data['azd_id'])
//                ->where('uid',$this->userId)
//                ->where('id',$inspectId)
//                ->value('inspect_code');
            // 判断巡检单是否存在
            if(empty($inspect_code)){
                $this->error('巡检单不存在！');
            }
            if ($xunj['inspect_status'] == 2){
                $this->error('巡检单已提交，不能修改');
            }
            // 查询巡检单编号
            $list['inspect_code'] = $inspect_code;
            // 赋值巡检单ID
            $list['inspectId'] = $inspectId;
        } else {
            // 赋值巡检单编号
            $list['inspect_code'] = $this->nonceStr(); // 生成巡检编码
        }
        // 品牌
        $list['brand'] = '海信';
        // 返回信息
        $this->success('操作成功！', $list);
    }

    /**
     * 安装单巡检添加+编辑-赋值
     * @param []
     * @return []
     * @date 2019/12/17 4:31 PM
     */
    public function inspectionAddInfo()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspect_code.require' => '巡检单编号',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id' => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id' => $this->request->get('azd_id'), // 获取安装单编号
            'inspect_code' =>  $this->nonceStr(), // 获取巡检单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        //dump($result);die;
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 获取安装单信息
        $hise_service_projec_info = Db::name('rl_hise_service_project')->where('hise_service_project_id', $data['hise_service_project_id'])->field('gczt,jxs,type')->find();
        // 安装单状态格式化
        $data['status_text'] = $this->install_status_new[$hise_service_projec_info['gczt']];
        // 获取所有节点类型
        $list = Db::name('rl_type')
            ->where('type_id', 'in', $hise_service_projec_info['type'])
            ->field('type_id,type_name')
            ->order('type_id ASC')
            ->select()
            ->toArray();
        // 遍历获取安装单已提交的节点
        foreach ($list as $key => $val) {
            // 获取已提交的节点
            $process = Db::table('cmf_rl_hise_service_pro_process')
                ->alias('p')
                ->join('rl_node n', 'p.azjd = n.node_id')
                ->where('gc_azdid', $data['azd_id'])
                ->where('cplp', $val['type_id'])
                ->field('n.node_id,n.node_name')
                ->order('p.create_time ASC')
                ->select()
                ->toArray();
            // 获取已提交的节点
            $list[$key]['process'] = $process;
        }
        // 获取巡检单ID
        $inspectId = input('get.inspectId');
        // 判断巡检ID是否存在
        if($inspectId){
            // 获取巡检单编号
            $inspectInfo = Db::name('rl_inspect')
                ->where('hid',$data['hise_service_project_id'])
                ->where('azd_id',$data['azd_id'])
                ->where('uid',$this->userId)
                ->where('id',$inspectId)
                ->field('fraction,is_problem,conclusion,is_model,checking_comment')
                ->find();
            // 评分总数
            $data['fraction'] = $inspectInfo['fraction'];
            // 是否存在问题
            $data['is_problem'] = $inspectInfo['is_problem'];
            // 巡检结论及工程存在的问题
            $data['conclusion'] = $inspectInfo['conclusion'];
            // 机型一致性
            $data['is_model'] = $inspectInfo['is_model'];
            //验证结论
            $data['checking_comment'] = $inspectInfo['checking_comment'];
            // 获取巡检节点数据
            $nodeList = Db::name('rl_inspect_step')
                ->where('tid',$inspectId)
                ->field('type,node,specs_pic,specs_pic_two,specs_pic_three,no_specs_pic,no_specs_pic_two,no_specs_pic_three')
                ->order('type ASC')
                ->select()
                ->toArray();
            //验证巡检信息
            foreach ($nodeList as $key => $value) {
                $checking = $this->inspectstepchecking($inspectId,$value['node']);
                $nodeList[$key]['checking'] = $checking;
            }
            // 巡检节点
            $data['nodeList'] = $nodeList;
        } else {
            // start***************************
            // 保存巡检单信息
            $inspectId = $this->xunJianAddInfo($data);
            // 判断是否保存成功
            if(empty($inspectId)){
                $this->error('意外的操作，巡检单保存失败');
            }
            // end***************************
        }
        // 赋值巡检单ID
        $data['inspectId'] = $inspectId;
        // 赋值节点信息
        $data['list'] = $list;
        // 返回信息
        $this->success('操作成功！', $data);
    }

    /**
     * 巡检信息新增
     * @param []
     * @return []
     * @date 2019/12/20 11:55 AM
     */
    protected function xunJianAddInfo($data)
    {
        // 获取巡检单编号
        $inspectId = Db::name('rl_inspect')
            ->where('hid',$data['hise_service_project_id'])
            ->where('azd_id',$data['azd_id'])
            ->where('uid',$this->userId)
            ->where('inspect_code',$data['inspect_code'])
            ->value('id');
        // 判断是否已存在该巡检单
        if(empty($inspectId)){
            // 巡检单参数
            $map['hid'] = $data['hise_service_project_id']; // 安装单ID
            $map['uid'] = $this->userId; // 用户ID
            $map['azd_id'] = $data['azd_id']; // 安装单编号
            $map['inspect_status'] = 1; // 巡检状态 1:初始,2:提交
            $map['progress'] = Db::name('rl_hise_service_project')->where('hise_service_project_id', $data['hise_service_project_id'])->value('gczt'); // 工程进度
            $map['vrct_status'] = 1; // 验证状态 1:未验证,2:已验证
            $time = time(); // 获取当前时间
            $map['inspect_time'] = $time; // 巡检时间
            $map['create_time'] = $time; // 创建时间
            $map['update_time'] = $time; // 更新时间
            $map['u_name'] = isset($this->user['user_name']) ? $this->user['user_name'] : '';
            $map['inspect_code'] = $data['inspect_code']; // 巡检编码
            $map['uPhone'] = Db::table('cmf_rl_zsms_user_query')->where('zsms_user_query_id',$this->userId)->value('user_id'); // 巡检人手机号******
            // 添加巡检单数据
            $inspectId = Db::name('rl_inspect')->insertGetId($map);
            // 返回巡检ID
            return $inspectId;
        } else {
            return $inspectId;
        }
    }

    /**
     * 获取巡检节点图片
     * @param []
     * @return []
     * @date 2019/12/21 11:42 AM
     */
    public function inspectionNodePic()
    {
        // 验证的规则
        $rule = [
            'type_id' => 'require',
            'node_id' => 'require',
            'inspectId' => 'require',

        ];
        // 错误的消息
        $msg = [
            'type_id.require' => '节点类型ID不存在',
            'node_id.require' => '子节点ID不存在',
            'inspectId.require' => '巡检单主键ID不存在',

        ];
        // 获取的数据
        $data = [
            'type_id' => $this->request->get('type_id'), // 获取节点类型ID
            'node_id' => $this->request->get('node_id'), // 获取子节点ID
            'inspectId' => $this->request->get('inspectId'), // 获取巡检单主键ID
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 获取巡检节点图片
        $nodeList = Db::name('rl_inspect_step')
            ->where('tid',$data['inspectId'])
            ->where('type',$data['type_id'])
            ->where('node',$data['node_id'])
            ->field('specs_pic,specs_pic_two,specs_pic_three,no_specs_pic,no_specs_pic_two,no_specs_pic_three')
            ->find();
        // 赋值节点信息
        $list = $nodeList;
        // 返回信息
        $this->success('操作成功！', $list);
    }

    /**
     * 上传巡检单节点图片
     * @param []
     * @return []
     * @date 2019/12/21 12:06 PM
     */
    public function inspectionUploadPic()
    {
        // 获取上传文件
        $file = $this->request->file('image');
        $place = $this->request->param('place','是空的，没有值');
        // 判断图片是否存在
        if (empty($file)) {
            $this->error('请选择上传图片');
        }
        // 获取当前时间
        $date = date('Ymd',time());
        // 移动到框架应用根目录/uploads/ 目录下 3MB
        $info = $file->rule('md5')->validate(['ext'=>'jpg,png,gif,jpeg'])->move( '../uploads/inspect/'.$date);
        // 判断图片是否上传成功
        if($info){
            // 成功上传后 获取上传信息
            $orpath = ROOT_PATH.'public/uploads/inspect/'.$date.'/'.$info->getSaveName();
            $logo = ROOT_PATH.'logo.png';
            // 旋转图像
            $logo_rotate = \think\Image::open($logo)->rotate(-45)->save(ROOT_PATH.'public/uploads/logo_45.png');
            $logo_45 = ROOT_PATH.'public/uploads/logo_45.png';
            // 添加水印
            $this->water_all($orpath ,$logo_45, $orpath, 15);
            $image = \think\Image::open($orpath);
            $image->text($place."\n".date('Y-m-d H:i:s'),ROOT_PATH.'msyh.ttc',14,'#FF0000',9,0,0)->save($orpath);

            $list['path'] = '/uploads/inspect/'.$date.'/'.$info->getSaveName();
            $this->success('操作成功！', $list);
        }else{
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }
    /**
     * 添加全屏水印
     * @param  [type] $origin [原图]
     * @param [type] $[logo] [水印图片]
     * @param [type] $[pathname] [保存路径]
     * @param [type] $[opacity] [透明度]
     * @return [type]         [description]
     */
    public function water_all($origin,$logo,$pathname,$opacity = 20)
    {
        $im = imagecreatefromstring(file_get_contents($origin));
        //获取水印源
        $watermark = imagecreatefromstring(file_get_contents($logo));
        //获取图、水印 宽高类型
        list($bgWidth, $bgHight, $bgType) = getimagesize($origin);
        list($logoWidth, $logoHight, $logoType) = getimagesize($logo);
        //定义平铺数据
        $x_length = $bgWidth - 10; //x轴总长度
        $y_length = $bgHight - 10; //y轴总长度
        //创建透明画布 伪白色
        $w = imagesx($watermark);
        $h = imagesy($watermark);
        $cut = imagecreatetruecolor($w,$h);
        $white = imagecolorallocatealpha($cut, 255,255,255,0);
        imagefill( $cut, 0, 0, $white );
        //整合水印
        imagecopy($cut, $watermark, 0, 0, 0, 0, $w, $h);
        //循环平铺水印
        for ($x = 0; $x < $x_length; $x++)
        {
            for ($y = 0; $y < $y_length; $y++) {
                imagecopymerge($im, $cut, $x, $y, 0, 0, $logoWidth, $logoHight, $opacity);
                $y += $logoHight;
            }
            $x += $logoWidth;
        }
        //header("Content-type:image/png");
        // imagejpeg 的第二个参数不传, 默认是显示图片
        imagejpeg($im,$pathname);
        empty($im) || imagedestroy($im);
    }

    /**
     * 安装单巡检添加+编辑-保存
     * @param []
     * @return []
     * @date 2019/12/18 6:42 PM
     */
    public function inspectionAddSubt()
    {
        // 验证的规则
        $rule = [
            'inspectId' => 'require',
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'fraction' => 'require',
            'is_problem' => 'require',
            'conclusion' => 'require',
            'is_model' => 'require',
        ];
        // 错误的消息
        $msg = [
            'inspectId.require' => '巡检单ID不存在',
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'fraction.require' => '请填写评分',
            'is_problem.require' => '请选择是否存在问题',
            'conclusion.require' => '请填写结论及工程存在的问题',
            'is_model.require' => '请选择机型是否一致',
        ];
        // 获取的数据
        $inspectid = $this->request->post('inspectId');
        $inspectCode = Db::name('rl_inspect')->where('id',$inspectid)->value('inspect_code');
        $data = [
            'inspectId' => $this->request->post('inspectId'), // 获取巡检单ID
            'inspectCode' => $inspectCode, // 获取巡检单编号
            'hise_service_project_id' => $this->request->post('hise_service_project_id'), // 安装单主键ID
            'azd_id' => $this->request->post('azd_id'), // 获取安装单编号
            'fraction' => $this->request->post('fraction'), // 获取评分总数
            'is_problem' => $this->request->post('is_problem'), // 获取是否存在问题
            'conclusion' => $this->request->post('conclusion'), // 获取结论及工程存在的问题
            'is_model' => $this->request->post('is_model'), // 获取机型是否一致
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 打分 节点*******************************
        // 获取当前时间
        $time = time();
        // 获取打分数据
        $scoringList = input('post.scoringList','','strip_tags');
        // 格式化json数据
        $scoringListArray = json_decode($scoringList,true);
        // 判断打分是否为空
        if($scoringListArray){
            // 遍历生成巡检评分数据
            foreach ($scoringListArray as $key=>$val) {
                // 判断是否传参正确
                if(empty($val['number']) && empty($val['deduction']) && empty($val['id'])){
                    $this->error('意外的操作，评分传参格式错误！');
                }
                // 巡检打分所需参数数据
                $scoredata = Db::name('rl_inspect_score')->where('id',$val['id'])->find();

                $dataScor[$key]['tid'] = $data['inspectId']; // 巡检单号
                $dataScor[$key]['hid'] = $data['hise_service_project_id']; // 安装单号
                $dataScor[$key]['create_time'] = $time; // 创建时间
                $dataScor[$key]['update_time'] = $time; // 创建时间
                $dataScor[$key]['number'] = $val['number']; // 评分序号
                $dataScor[$key]['sid'] = $val['id']; // 评分序号
                $dataScor[$key]['deduction'] = isset($val['deduction'])?$val['deduction']:0; // 扣分数

                $dataScor[$key]['weight'] = $scoredata['weight']; // 权重
                $dataScor[$key]['name'] = $scoredata['name']; // 项目
                $dataScor[$key]['content'] = $scoredata['content']; // 主要内容
                $dataScor[$key]['fraction'] = $scoredata['fraction']; // 分数
                $dataScor[$key]['inspect_code'] = $data['inspectCode']; // 巡检单号
            }
        }
        // 获取节点数据
        $nodeList = input('post.nodeList','','strip_tags');
        // 格式化json数据
        $nodeListArray = json_decode($nodeList,true);
        // 判断节点是否为空
        if(empty($nodeListArray)){
            $this->error('请填写节点信息');
        } else {
            // 遍历生成巡检节点数据
            $nodearr = Db::name('rl_node')->select()->column('node_name','node_id');
            foreach ($nodeListArray as $key=>$val) {
                // 判断是否传参正确
                if(empty($val['type']) && empty($val['node']) && empty($val['is_problem'])){
                    $this->error('意外的操作，节点传参格式错误！');
                }
                $product_type = Db::name('rl_type')->where('type_id',$val['type'])->value('product_type');
                // 巡检节点所需参数数据
                $dataStep[$key]['tid'] = $data['inspectId']; // 巡检单号
                $dataStep[$key]['create_time'] = $time; // 创建时间
                $dataStep[$key]['update_time'] = $time; // 创建时间
                $dataStep[$key]['type'] = $val['type']; // 类型ID
                $dataStep[$key]['type_bm'] = $product_type; // 类型编码
                $dataStep[$key]['node'] = $val['node']; // 节点ID
                $dataStep[$key]['inspect_code'] = $data['inspectCode']; // 巡检单号
                $dataStep[$key]['node_name'] = $nodearr[$val['node']]; // 节点名字
                $dataStep[$key]['specs_pic'] = $val['specs_pic']; // 规范照片1
                $dataStep[$key]['specs_pic_two'] = $val['specs_pic_two']; // 规范照片2
                $dataStep[$key]['specs_pic_three'] = $val['specs_pic_three']; // 规范照片3
                $dataStep[$key]['no_specs_pic'] = $val['no_specs_pic']; // 不规范照片1
                $dataStep[$key]['no_specs_pic_two'] = $val['no_specs_pic_two']; // 不规范照片2
                $dataStep[$key]['no_specs_pic_three'] = $val['no_specs_pic_three']; // 不规范照片3
                $dataStep[$key]['is_problem'] = $data['is_problem']; // 是否存在问题
            }
        }
        // 启动事务
        Db::startTrans();
        // 获取评分数量
        $countScor = Db::name('rl_inspect_score_scoring')->where('tid', $data['inspectId'])->count();
        // 获取节点数量
        $countStep = Db::name('rl_inspect_step')->where('tid', $data['inspectId'])->count();
        // 获取当前类型 1=>新增 2=>编辑
        $type = $this->request->post('type',1);
        // 判断
        if($type == 1){
            // 判断打分是否为空
            if(empty($scoringListArray)){
                // 回滚事务
                Db::rollback();
                $this->error('请进行打分');
            }
            // 判断是否已添加评分
            if(empty($countScor)){
                // 新增巡检打分表数据
                $result1 = Db::name('rl_inspect_score_scoring')->insertAll($dataScor, true);
            } else {
                $result1 = true;
            }
            // 判断是否已添加节点
            if(empty($countStep)){
                // 新增巡检单节点数据
                $result2 = Db::name('rl_inspect_step')->insertAll($dataStep, true);
            } else {
                $result2 = true;
            }
        } else {
            // 判断打分是否为空
            if(empty($scoringListArray)){
                $result1 = true;
            } else {
                // 查询巡检单评分数据
                $listScor = Db::name('rl_inspect_score_scoring')
                    ->where('tid', $data['inspectId'])
                    ->where('hid', $data['hise_service_project_id'])
                    ->field('sid as id,number,deduction')
                    ->order('number ASC')
                    ->select()
                    ->toArray();
                // 键值反转
                $listScorColumn = array_column($listScor, 'deduction', 'id');
                // 遍历更新需要修改的评分
                foreach ($dataScor as $key=>$val) {
                    if($listScorColumn[$val['sid']] != $val['deduction']){
                        $result1 = Db::name('rl_inspect_score_scoring')
                            ->where('tid', $data['inspectId'])
                            ->where('hid', $data['hise_service_project_id'])
                            ->where('sid', $val['sid'])
                            ->data(['deduction' => $val['deduction'],'update_time' => $time])
                            ->update();
                        // 修改失败
                        if(!$result1){
                            // 回滚事务
                            Db::rollback();
                            $this->error('巡检评分保存失败');
                        }
                    } else {
                        $result1 = true;
                    }
                }
            }
            // 判断是否已添加节点
            if(empty($countStep)){
                // 新增巡检单节点数据
                $result2 = Db::name('rl_inspect_step')->insertAll($dataStep, true);
            } else {
                // 获取巡检节点数据
                $nodeList = Db::name('rl_inspect_step')
                    ->where('tid',$data['inspectId'])
                    ->field('id,tid,type,node,specs_pic,specs_pic_two,specs_pic_three,no_specs_pic,no_specs_pic_two,no_specs_pic_three')
                    ->order('type ASC')
                    ->select()
                    ->toArray();
                // 遍历判断是否为修改或者是否为新增
                foreach ($dataStep as $key=>$value) {
                    // 查询巡检节点信息
                    $id = Db::name('rl_inspect_step')
                        ->where('tid',$value['tid'])
                        ->where('type',$value['type'])
                        ->where('node',$value['node'])
                        ->value('id');
                    // 判断巡检节点是否存在
                    if($id){
                        // 修改所需参数
                        $mapStepPic['update_time'] = $time;
                        $mapStepPic['is_problem'] = $value['is_problem'];
                        $mapStepPic['specs_pic'] = $value['specs_pic'];
                        $mapStepPic['specs_pic_two'] = $value['specs_pic_two'];
                        $mapStepPic['specs_pic_three'] = $value['specs_pic_three'];
                        $mapStepPic['no_specs_pic'] = $value['no_specs_pic'];
                        $mapStepPic['no_specs_pic_two'] = $value['no_specs_pic_two'];
                        $mapStepPic['no_specs_pic_three'] = $value['no_specs_pic_three'];
                        // 已有则进行编辑修改
                        $result2 = Db::name('rl_inspect_step')
                            ->where('id',$id)
                            ->data($mapStepPic)
                            ->update();
                        // 修改失败
                        if(!$result2){
                            // 回滚事务
                            Db::rollback();
                            $this->error('巡检节点保存失败');
                        }
                    } else {
                        // 没有节点则进行新增
                        $result2 = Db::name('rl_inspect_step')->insert($value, true);
                        // 修改失败
                        if(!$result2){
                            // 回滚事务
                            Db::rollback();
                            $this->error('巡检节点保存失败');
                        }
                    }
                }
                // 遍历生成 in 拼接值
                foreach ($dataStep as $key=>$value) {
                    $dataStep[$key]['in'] = $value['tid'].'.'.$value['type'].'.'.$value['node'];
                }
                // 键值反转
                $dataStepT = array_column($dataStep,NULL,'in');
                // 定义变量
                $ids = '';
                // 遍历获取无用的节点信息
                foreach ($nodeList as $key=>$val) {
                    $in = $val['tid'].'.'.$val['type'].'.'.$val['node'];
                    if(!array_key_exists($in,$dataStepT)){
                        $ids[] = $val['id'];
                    }
                }
                // 判断是否有无用的节点
                if($ids){
                    // 删除无用的节点信息
                    $result4 = Db::name('rl_inspect_step')->where('id','in',$ids)->delete();
                    // 修改失败
                    if(!$result4) {
                        // 回滚事务
                        Db::rollback();
                        $this->error('巡检节点保存失败');
                    }
                }
            }
        }
        // 打分 节点 end*************************
        // 巡检单所需参数数据
        $mapIns['progress'] = Db::name('rl_hise_service_project')->where('hise_service_project_id', $data['hise_service_project_id'])->value('gczt'); // 工程进度
        $mapIns['is_problem'] = $data['is_problem']; // 是否存在问题 0:否,1:是
        $mapIns['conclusion'] = $data['conclusion']; // 巡检结论及工程存在的问题
        $mapIns['is_model'] = $data['is_model']; // 机型一致性 0:不一致,1:一致
        $mapIns['fraction'] = $data['fraction']; // 评分总数
        $mapIns['update_time'] = $time; // 更新时间
        $mapIns['u_name'] = isset($this->user['user_name']) ? $this->user['user_name'] : ''; // 巡检人
        // 修改巡检信息
        $result3 = Db::name('rl_inspect')
            ->where('id', $data['inspectId'])
            ->where('hid',$data['hise_service_project_id'])
            ->where('azd_id',$data['azd_id'])
            ->where('uid',$this->userId)
            ->data($mapIns)
            ->update();
        // 判断是否添加成功
        if($result1 && $result2 && $result3){
            // 提交事务
            Db::commit();
            // 返回信息
            $this->success('操作成功！',['inspectId'=>$data['inspectId']]);
        } else {
            // 回滚事务
            Db::rollback();
            $this->error('操作失败！');
        }
    }

    /**
     * 安装单巡检列表
     * @param []
     * @return []
     * @date 2019/12/13 10:45 AM
     */
    public function inspectionList()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id'  => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->get('azd_id'), // 获取安装单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        $user=$this->userId;
        // 查询巡检数据 ******
        $inspectList = Db::name('rl_inspect')
            ->where('hid', $data['hise_service_project_id'])
            ->where('azd_id', $data['azd_id'])
            ->where(function($inspectList)use($user){
                $inspectList->whereOr('uid',$user);
                $uPhone=Db::table('cmf_rl_zsms_user_query')->where('zsms_user_query_id',$user)->value('mobilphone');
                 $inspectList->whereOr('uPhone',$uPhone);
            })
            ->where('uid',$this->userId)
            ->field('id as inspectId,inspect_code,uid,inspect_time,inspect_status,vrct_status,fraction')
            ->order('id desc')
            ->select()
            ->toArray();
     // dump($inspectList);die;
        // 遍历获取格式化巡检数据
        foreach ($inspectList as $key=>$value) {
            $inspectList[$key]['user_name'] = Db::name('rl_zsms_user_query')->where('ignore',0)->where('zsms_user_query_id', $value['uid'])->value('user_name');
            $inspectList[$key]['inspect_time'] = date('Y-m-d',$value['inspect_time']);
            unset($inspectList[$key]['uid']);
        }
        // 赋值巡检数据
        $data['list'] = $inspectList;
        $this->success('操作成功！', $data);
    }

    /**
     * 巡检单提交
     * @param []
     * @return []
     * @date 2019/12/21 5:37 PM
     */
    public function inspectionReferTo()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspectId' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspectId.require' => '巡检单主键ID不存在',
            'inspect_code.require' => '巡检单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id'  => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->get('azd_id'), // 获取安装单编号
            'inspectId' => $this->request->get('inspectId'), // 获取巡检单主键ID
            'inspect_code' => $this->request->get('inspect_code'), // 获取巡检单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 查询巡检单信息
        $info = Db::name('rl_inspect')
            ->where('hid', $data['hise_service_project_id'])
            ->where('azd_id', $data['azd_id'])
            ->where('id', $data['inspectId'])
            ->where('inspect_code', $data['inspect_code'])
            ->where('uid',$this->userId)
            ->field('id,inspect_status,vrct_status,is_problem,fraction')
            ->find();
        // 判断是否存在
        if(empty($info)){
            $this->error('巡检单不存在');
        }
        // 判断巡检单是否提交
        if($info['inspect_status'] == '2'){
            $this->error('巡检单已提交，不可重复提交');
        }
        // 判断巡检单是否有评分
        if(empty($info['fraction'])){
            $this->error('请先进行评分');
        }
        // 获取节点数量
        $countStep = Db::name('rl_inspect_step')->where('tid', $data['inspectId'])->count();
        // 判断是否有节点信息
        if(empty($countStep)){
            $this->error('请先上传节点信息');
        }
        // 巡检状态设置为已提交
        $map['inspect_status'] = 2;
        // 当前时间
        $map['inspect_time'] = time();
        // 判断是否存在问题
        if($info['is_problem'] != '1'){
            // 不存在问题验证状态设置为已验证
            $map['vrct_status'] = 2;
        }
        // 更新巡检单状态为提交
        $result = Db::name('rl_inspect')
            ->where('id', $info['id'])
            ->data($map)
            ->update();
        // 判断是否修改成功
        if($result){
            Db::name('rl_hise_service_project')->where('hise_service_project_id', $data['hise_service_project_id'])->update(['sfxjbs'=>1]);
            $this->success('巡检单提交成功');
        } else {
            $this->error('巡检单提交失败');
        }
    }

    /**
     * 巡检单删除
     * @param []
     * @return []
     * @date 2019/12/21 6:06 PM
     */
    public function inspectionDelete()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspectId' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspectId.require' => '巡检单主键ID不存在',
            'inspect_code.require' => '巡检单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id'  => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->get('azd_id'), // 获取安装单编号
            'inspectId' => $this->request->get('inspectId'), // 获取巡检单主键ID
            'inspect_code' => $this->request->get('inspect_code'), // 获取巡检单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 查询巡检单信息
        $info = Db::name('rl_inspect')
            ->where('hid', $data['hise_service_project_id'])
            ->where('azd_id', $data['azd_id'])
            ->where('id', $data['inspectId'])
            ->where('inspect_code', $data['inspect_code'])
            ->where('uid',$this->userId)
            ->field('id,inspect_status,vrct_status,is_problem')
            ->find();
        // 判断是否存在
        if(empty($info)){
            $this->error('巡检单不存在');
        }
        // 判断巡检单是否可以删除
        if($info['inspect_status'] == '2'){
            $this->error('巡检单已提交，不可删除');
        }
        // 启动事务
        Db::startTrans();
        // 删除巡检单
        $result1 = Db::name('rl_inspect')->where('id',$info['id'])->delete();
        // 获取评分数量
        $countScor = Db::name('rl_inspect_score_scoring')->where('tid', $data['inspectId'])->count();
        // 判断评分是否存在
        if($countScor){
            // 删除该巡检单的评分信息
            $result2 = Db::name('rl_inspect_score_scoring')->where('tid',$data['inspectId'])->delete();
        } else {
            $result2 = true;
        }
        // 获取节点数量
        $countStep = Db::name('rl_inspect_step')->where('tid', $data['inspectId'])->count();
        // 判断节点是否存在
        if($countStep){
            // 删除该巡检单的节点信息
            $result3 = Db::name('rl_inspect_step')->where('tid',$data['inspectId'])->delete();
        } else {
            $result3 = true;
        }
        // 判断是否删除成功
        if($result1 && $result2 && $result3){
            // 提交事务
            Db::commit();
            $this->success('巡检单删除成功');
        } else {
            // 回滚事务
            Db::rollback();
            $this->error('巡检单删除失败');
        }
    }

    /**
     * 巡检单验证
     * @param []
     * @return []
     * @date 2019/12/21 6:24 PM
     */
    public function  inspectionChecking()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspectId' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspectId.require' => '巡检单主键ID不存在',
            'inspect_code.require' => '巡检单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id'  => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->get('azd_id'), // 获取安装单编号
            'inspectId' => $this->request->get('inspectId'), // 获取巡检单主键ID
            'inspect_code' => $this->request->get('inspect_code'), // 获取巡检单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 查询巡检单信息
        $info = Db::name('rl_inspect')
            ->where('hid', $data['hise_service_project_id'])
            ->where('azd_id', $data['azd_id'])
            ->where('id', $data['inspectId'])
            ->where('inspect_code', $data['inspect_code'])
            ->where('uid',$this->userId)
            ->field('id,inspect_status,vrct_status,is_problem')
            ->find();
        // 判断是否存在
        if(empty($info)){
            $this->error('巡检单不存在');
        }
        // 判断巡检单是否已提交
        if($info['inspect_status'] != '2'){
            $this->error('请先提交巡检单再进行验证');
        }
        // 判断巡检单是否存在问题
        if($info['is_problem'] != '1'){
            $this->error('巡检单不存在问题，不可进行验证');
        }
        // 判断巡检单是否已验证
        if($info['vrct_status'] == '2'){
            $this->error('巡检单已验证，不可重复验证');
        }
        $this->success('操作成功！');
    }

    /**
     * 巡检单验证-节点查看
     * @param []
     * @return []
     * @date 2019/12/21 6:55 PM
     */
    public function inspectionNodeView()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspectId' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspectId.require' => '巡检单主键ID不存在',
            'inspect_code.require' => '巡检单编号不存在',
        ];
        // 获取的数据
        $data = [
            'hise_service_project_id'  => $this->request->get('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->get('azd_id'), // 获取安装单编号
            'inspectId' => $this->request->get('inspectId'), // 获取巡检单主键ID
            'inspect_code' => $this->request->get('inspect_code'), // 获取巡检单编号
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 获取巡检节点数据
        $listStep = Db::name('rl_inspect_step')
            ->where('tid',$data['inspectId'])
            ->where('is_problem',1)
            ->where('no_specs_pic','<>','')
            ->field('type,node,no_specs_pic,no_specs_pic_two,no_specs_pic_three')
            ->order('type ASC')
            ->select()
            ->toArray();
        // 遍历获取类型节点名称
        foreach ($listStep as $key=>$value) {
            // 节点类型名称
            $listStep[$key]['type_name'] = Db::name('rl_type')->where('type_id', $value['type'])->value('type_name');
            // 子节点名称
            $listStep[$key]['node_name'] = Db::name('rl_node')->where('node_id', $value['node'])->where('type_id',$value['type'])->value('node_name');
            unset($listStep[$key]['type']);
            unset($listStep[$key]['node']);
        }
        // 赋值巡检单节点数据
        $this->success('操作成功！',$listStep);
    }

    /**
     * 巡检单验证-提交数据
     * @param []
     * @return []
     * @date 2019/12/21 7:18 PM
     */
    public function inspectionCheckingSub()
    {
        // 验证的规则
        $rule = [
            'hise_service_project_id' => 'require',
            'azd_id' => 'require',
            'inspectId' => 'require',
            'inspect_code' => 'require',
        ];
        // 错误的消息
        $msg = [
            'hise_service_project_id.require' => '安装单主键ID不存在',
            'azd_id.require' => '安装单编号不存在',
            'inspectId.require' => '巡检单主键ID不存在',
            'inspect_code.require' => '巡检单编号不存在',
        ];
        // 获取的数据
        $inspect_code = $this->request->post('inspect_code');
        $data = [
            'hise_service_project_id'  => $this->request->post('hise_service_project_id'), // 安装单主键ID
            'azd_id'   => $this->request->post('azd_id'), // 获取安装单编号
            'inspectId' => $this->request->post('inspectId'), // 获取巡检单主键ID
            'inspect_code' => $inspect_code, // 获取巡检单编号
            'conclusion' => $this->request->post('conclusion'), // 获取验证结论
            'checking_name' => $this->request->post('checking_name'), // 获取验证人
            'checking_time' => $this->request->post('checking_time'), // 获取验证时间
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if(!$result){
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 获取类型
        $type = input('post.type','1');
        // 判断类型 1=>赋值 2=>提交
        if($type == '1'){
            // 获取巡检节点数据
            $listStep = Db::name('rl_inspect_step')
                ->where('tid',$data['inspectId'])
                ->where('is_problem',1)
                ->where('no_specs_pic','<>','')
                ->field('type,node')
                ->order('type ASC')
                ->select()
                ->toArray();
            // 遍历获取类型节点名称
            foreach ($listStep as $key=>$value) {
                // 节点类型名称
                $listStep[$key]['type_name'] = Db::name('rl_type')->where('type_id', $value['type'])->value('type_name');
                // 子节点名称
                $listStep[$key]['node_name'] = Db::name('rl_node')->where('node_id', $value['node'])->where('type_id',$value['type'])->value('node_name');
            }
            // 赋值所需参数
            $info['hise_service_project_id'] = $data['hise_service_project_id'];
            $info['azd_id'] = $data['azd_id'];
            $info['inspectId'] = $data['inspectId'];
            $info['inspect_code'] = $data['inspect_code'];
            // 获取验证结论
            $info['conclusion'] = Db::name('rl_inspect')
                ->where('id',$data['inspectId'])
                ->value('conclusion');
            // 赋值验证节点数据
            $info['list'] = $listStep;
            $info['user_name'] = $this->user['user_name'];
            $this->success('操作成功！',$info);
        } else {
            // 判断验证信息是否存在
            if(empty($data['conclusion'])){
                $this->error('请输入验证结论');
            }
            if(empty($data['checking_name'])){
                $this->error('请输入验证人');
            }
            if(empty($data['checking_time'])){
                $this->error('请输入验证时间');
            }
            // 启动事务
            Db::startTrans();
            // 获取验证数据
            $checkingList = input('post.checkingList','','strip_tags');
            // 格式化json数据
            $checkingListArray = json_decode($checkingList,true);
            // 当前时间
            $time = time();
            // 判断验证数据是否存在
            if(empty($checkingListArray)){
                $this->error('请上传验证图片');
            } else {
                // 获取验证节点数量
                $countScor = Db::name('rl_inspect_step_checking')->where('tid', $data['inspectId'])->count();
                // 判断评分是否存在
                if(!empty($countScor)){
                    $result1 = true;
                } else {
                    // 遍历生成验证节点数据
                    foreach ($checkingListArray as $key=>$val) {
                        // 判断是否传参正确
                        if(empty($val['type']) && empty($val['node'])){
                            $this->error('意外的操作，验证节点传参格式错误！');
                        }
                        // 验证节点所需参数数据
                        $node_name = Db::name('rl_node')->where('node_id', $val['node'])->where('type_id',$val['type'])->value('node_name');
                        $product_type = Db::name('rl_type')->where('type_id',$val['type'])->value('product_type');
                        $dataStep[$key]['tid'] = $data['inspectId']; // 巡检单号
                        $dataStep[$key]['uid'] = $this->userId; // 用户ID
                        $dataStep[$key]['create_time'] = $time; // 创建时间
                        $dataStep[$key]['update_time'] = $time; // 修改时间
                        $dataStep[$key]['type'] = $val['type']; // 类型ID
                        $dataStep[$key]['type_bm'] = $product_type; // 类型编码
                        $dataStep[$key]['node'] = $val['node']; // 节点ID
                        $dataStep[$key]['inspect_code'] = $inspect_code; //巡检单号
                        $dataStep[$key]['node_name'] = $node_name; // 节点名称
                        $dataStep[$key]['pic_one'] = $val['pic_one']; // 验证图片1
                        $dataStep[$key]['pic_two'] = $val['pic_two']; // 验证图片2
                        $dataStep[$key]['pic_three'] = $val['pic_three']; // 验证图片3
                        $dataStep[$key]['pic_four'] = $val['pic_four']; // 验证图片4
                        $dataStep[$key]['pic_five'] = $val['pic_five']; // 验证图片5
                    }
                    // 新增验证单节点数据
                    $result1 = Db::name('rl_inspect_step_checking')->insertAll($dataStep, true);
                }
            }
            // 更新参数
            $dataIns['vrct_status'] = 2;
            $dataIns['is_problem'] = 0;
            $dataIns['checking_name'] = $data['checking_name'];
            $dataIns['checking_time'] = strtotime($data['checking_time']);
            $dataIns['checking_comment'] = $data['conclusion'];
            $dataIns['update_time'] = $time;
            // 更新巡检单状态信息
            $result2 = Db::name('rl_inspect')
                ->where('id', $data['inspectId'])
                ->data($dataIns)
                ->update();
            // 判断是否新增和修改成功
            if($result1 && $result2){
                // 提交事务
                Db::commit();
                $this->success('巡检单验证提交成功');
            } else {
                // 回滚事务
                Db::rollback();
                $this->error('巡检单验证提交失败');
            }
        }
    }


    /**
     * 获取巡检验证数据
     * @param []
     * @return []
     * @date 2019/12/19 10:10 AM
     */
    public function inspectstepchecking($inspectId,$node) {
        $countScor = Db::name('rl_inspect_step_checking')->where('node', $node)->where('tid', $inspectId)->find();
        if($countScor)
        {
            return $countScor;
        }
        return [];
    }

    /**
     * 获取随机编码
     * @param []
     * @return []
     * @date 2019/12/19 10:10 AM
     */
    public function nonceStr() {
        static $seed = array(0,1,2,3,4,5,6,7,8,9);
        $str = '';
        for($i=0;$i<8;$i++) {
            $rand = mt_rand(0,count($seed)-1);
            $temp = $seed[$rand];
            $str .= $temp;
            unset($seed[$rand]);
            $seed = array_values($seed);
        }
        $time = date('Ymd',time());
        return $time.$str;
    }
}
